package com.cenxi.ipdemo1.utils.excel;


import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.util.*;

import java.util.List;
import java.util.Map;

/**
 * 公共的合并单元格类，支持自定义合并策略
 */
public class CustomMergeStrategy extends AbstractMergeStrategy {
    // 合并策略list
    private final List<CellRangeAddress> CELL_RANGE_ADDRESSS_LIST;
    // 通过有参数构造方法，设置合并策略
    public CustomMergeStrategy(List<CellRangeAddress> cellRangeAddresssList) {
        this.CELL_RANGE_ADDRESSS_LIST = cellRangeAddresssList;
    }

    /**
     * 重写合并策略方法
     * @param sheet sheet
     * @param cell cell
     * @param head head
     * @param relativeRowIndex relativeRowIndex
     */
    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
            // 将自定义合并策略假如excel
            if (!CollectionUtils.isEmpty(CELL_RANGE_ADDRESSS_LIST)) {
                for (CellRangeAddress item : CELL_RANGE_ADDRESSS_LIST) {
                    sheet.addMergedRegionUnsafe(item);
                }
            }
    }
}


